// Verify WeChat Mini Program setup
const fs = require('fs');
const path = require('path');

function checkFileExists(filePath) {
  try {
    return fs.existsSync(filePath);
  } catch (err) {
    return false;
  }
}

function verifyStructure() {
  console.log('Verifying WeChat Mini Program structure...\n');
  
  const requiredFiles = [
    'app.js',
    'app.json',
    'app.wxss',
    'project.config.json',
    'sitemap.json',
    'utils/api.js',
    'utils/location.js',
    'pages/route-planner/route-planner.js',
    'pages/route-planner/route-planner.wxml',
    'pages/route-planner/route-planner.wxss',
    'pages/route-planner/route-planner.json',
    'pages/navigation/navigation.js',
    'pages/navigation/navigation.wxml',
    'pages/navigation/navigation.wxss',
    'pages/navigation/navigation.json',
    'pages/music-library/music-library.js',
    'pages/music-library/music-library.wxml',
    'pages/music-library/music-library.wxss',
    'pages/music-library/music-library.json',
    'pages/footprint/footprint.js',
    'pages/footprint/footprint.wxml',
    'pages/footprint/footprint.wxss',
    'pages/footprint/footprint.json'
  ];
  
  let allExist = true;
  
  for (const file of requiredFiles) {
    if (checkFileExists(file)) {
      console.log(`✓ File exists: ${file}`);
    } else {
      console.log(`✗ Missing file: ${file}`);
      allExist = false;
    }
  }
  
  return allExist;
}

function verifyConfig() {
  console.log('\nVerifying configuration files...\n');
  
  try {
    // Check app.json
    const appJson = JSON.parse(fs.readFileSync('app.json', 'utf8'));
    console.log('✓ app.json is valid JSON');
    
    if (appJson.pages && appJson.pages.length > 0) {
      console.log(`✓ app.json defines ${appJson.pages.length} pages`);
    }
    
    // Check project.config.json
    const projectConfig = JSON.parse(fs.readFileSync('project.config.json', 'utf8'));
    console.log('✓ project.config.json is valid JSON');
    
    return true;
  } catch (err) {
    console.log(`✗ Configuration error: ${err.message}`);
    return false;
  }
}

console.log('='.repeat(60));
console.log('步语 (Buyu) Frontend Setup Verification');
console.log('='.repeat(60));
console.log('');

const structureOk = verifyStructure();
const configOk = verifyConfig();

if (structureOk && configOk) {
  console.log('\n' + '='.repeat(60));
  console.log('✓ Setup verification PASSED!');
  console.log('='.repeat(60));
  console.log('\nNext steps:');
  console.log('1. Open this directory in WeChat DevTools');
  console.log('2. Configure your AppID in project.config.json');
  console.log('3. Update API base URL in app.js');
  process.exit(0);
} else {
  console.log('\n' + '='.repeat(60));
  console.log('✗ Setup verification FAILED!');
  console.log('='.repeat(60));
  process.exit(1);
}
